<?php
/*********************************************************************\
*  Copyright (c) 1998-2013, TH. All Rights Reserved.
*  Author :liuxin
*  FName  :BaseController.class.php
*  Time   :2016/10/11 16:09:28
*  Remark :前台控制器基类
\*********************************************************************/
namespace Home\Controller;
use Think\Controller;
class BaseController extends Controller {

    /**
     * ps ：检查用户登录状态
     * Time：2016/04/06 16:11:00
     * @author liuxin
    */
    public function checkLogin($redirect = true){
        if ( !session("login_user")) {
            $cookie_token = cookie('cookie_token');
            if ($cookie_token) {
                $ret = D("User")->where("cookie_token = '%s' ",array($cookie_token))->field('user_id,username')->find();
                if ($ret && $ret['cookie_token_expire'] > time() ) {
                    $login_user['user_id'] = $ret['user_id'];
                    $login_user['username'] = $ret['username'];
                    session("login_user" , $login_user);
                    return $login_user ;

                }
            }
            if ($redirect) {
                $this->message("你尚未登录！",U('Home/Passport/login'));
                exit();
            }
        }else{
            return session("login_user") ;
        }
    }

    public function message($msg , $redirect = ''){
        $this->assign("msg" , $msg);
        $this->assign("redirect" , $redirect);
        $this->display ("Common/message");
    }

    public function typeaheadData(){
        $keyword = I('keyword');

        $where['username'] = array('like','%'.$keyword.'%');
        $data = D('User')->where($condition)->field('user_id,username')->select();
        // foreach ($data as $k => $v) {
        //     $ret[] = $v['username'];
        // }

        $this->ajaxReturn($data);
    }

    public function uploadImg(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        // 上传文件 
        $info   =   current($upload->upload($_FILES));
        $this->ajaxReturn(['success'=>true,'imgurl'=>$upload->rootPath.$info['savepath'].$info['savename']]);exit();
    }
}